def precompute_bresenham_deltas(table_size=1000):
    delta_table = {}
    for i in range(0, table_size + 1):
        slope = i / table_size 
        delta1 = 2 * slope # 斜率引起的变化量
        delta2 = delta1 - 2 # 决策变量更新需要参数
        delta_table[slope] = (delta1, delta2)
    print(delta_table)
    return delta_table

def BresenhamDrawLine(x1, y1, x2, y2, optimize=False, delta_table=None):
    points = []
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    # x方向的移动步幅
    sx = 1 if x2 > x1 else -1
    # y方向的移动步幅
    sy = 1 if y2 > y1 else -1

    if dx > dy:
        # x差值更大，以x为主轴进行画点的情况
        if optimize and delta_table is not None:
            slope = dy / dx if dx != 0 else float('inf')
            # 如果delta表中有该斜率，则使用优化的delta值
            if slope in delta_table and slope != 0:
                delta1, delta2 = delta_table[slope] 
                factor = 2 * dy / delta1
                delta1 *= factor
                delta2 *= factor
            else:
                # 未优化的delta值
                delta1, delta2 = 2 * dy, 2 * dy - 2 * dx
        else:
            delta1, delta2 = 2 * dy, 2 * dy - 2 * dx

        d = delta2 # 决策变量初始化
        y = y1
        for x in range(x1, x2, sx):
            points.append((x, y))
            # 按公式更新d
            if d > 0:
                y += sy
                d += delta2
            else:
                d += delta1

    else:
        # y差值更大，以y为主轴进行画点的情况
        d = 2 * dx - dy
        if optimize and delta_table is not None:
            slope = dx / dy if dy != 0 else float('inf')
            if slope in delta_table and slope != 0:
                delta1, delta2 = delta_table[slope] 
                factor = 2 * dx / delta1
                delta1 *= factor
                delta2 *= factor
            else:
                delta1, delta2 = 2 * dx, 2 * dx - 2 * dy
        else:
            delta1, delta2 = 2 * dx, 2 * dx - 2 * dy
    
        x = x1
        for y in range(y1, y2, sy):
            points.append((x, y))
            if d > 0:
                x += sx
                d += delta2
            else:
                d += delta1

    points.append((x2, y2)) # 加入终点
    return points
